//
//  HPSelectView.m
//  HPZZTong
//
//  Created by handpay on 14-8-19.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPSelectView.h"
#import "ZZTongDefine.h"

@interface HPSelectView ()

@property(nonatomic, strong) NSArray* dataArray;
@property(nonatomic, strong) UITableView* selectTableView;
@property(nonatomic, assign) id<HPSelectDelegate> delegate;
@property(nonatomic, strong) UIView* bgView;

@end

@implementation HPSelectView

- (id)initWithData:(NSArray*)array frame:(CGRect)frame delegate:(id<HPSelectDelegate>)dele{
    CGRect screenRect = [[UIScreen mainScreen]bounds];
    self = [super initWithFrame:screenRect];
    if (self) {
        
        self.backgroundColor = UIColorFromRGBA(0x000000, 0.75);
        
        _dataArray = array;
        
        _delegate = dele;
        
        
        if (frame.size.height <= screenRect.size.height) {
            frame.size.height = kCellHeight * _dataArray.count + 66;
            frame.origin.y = (screenRect.size.height - frame.size.height) / 2;
        }else{
            frame.size.height = screenRect.size.height;
            
        }
        _bgView = [[UIView alloc]initWithFrame:frame];
        _bgView.backgroundColor = kNormalBackgroundColor;
        _bgView.layer.cornerRadius =  8.0;
        
        [self addSubview:_bgView];
        
        UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(frame.origin.x, 15, frame.size.width - 20, 21)];
        label.text = @"请点击选择支付的卡";
        label.font = kNormalFont;
        label.textColor = kNormalFontColor;
        label.backgroundColor = [UIColor clearColor];
        
        [_bgView addSubview:label];
        
        frame.origin.y = 51;
        frame.size.height -= 66;
        frame.origin.x = 0;
        
        _selectTableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain];
        _selectTableView.delegate = self;
        _selectTableView.dataSource = self;
        _selectTableView.scrollEnabled = NO;
        
        [_bgView addSubview:_selectTableView];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    return self;
}
#pragma dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray* infoArr = [self.dataArray objectAtIndex:indexPath.row];
    
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
    
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [infoArr objectAtIndex:0];
    [cell addSubview:desLabel];
    
    
    return cell;
}
#pragma delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_delegate respondsToSelector:@selector(selectResult:)]) {
        NSArray* type = [_dataArray objectAtIndex:indexPath.row];
        [_delegate performSelector:@selector(selectResult:) withObject:type];
    }
    [self removeFromSuperview];
}
@end
